// 设置文件
import setting from '@/setting.js'
import { frameInRoutes } from '@/router/routes.js'
import storage from '@/utils/storage.js'
import store from '@/store/index'
import * as HttpServices from "@/utils/HttpServices";
import { getPayload, getLoginPayload } from "@/utils/server";
// import dbModules from './db'
// import db from "./db"
const ddmgConfig = window.ddmgConfig

export default {
  moduleName: "menu",
  namespaced: true,
  state() {
    return {
      // 从服务端返回的菜单树
      remoteMenu: storage.getStorage('menuTree') || [],
      // 顶栏菜单
      header: storage.getStorage('headerMenu') || [],
      // 侧栏菜单
      aside: [],
      // 侧边栏收缩
      asideCollapse: setting.menu.asideCollapse,
      // 侧边栏折叠动画
      asideTransition: setting.menu.asideTransition,
      // 激活的路由地址
      activeMenuPath: '',
      //当前侧边菜单激活下标
      asideActiveIndex: storage.getStorage('asideActiveIndex') || '',
      // 顶部导航栏下标
      activeIndex: '1',
      //面包屑
      breadList: []
    }
  },
  actions: {
    updateRemoteMenu({state,dispatch,commit},params = {}){
      const payload = getLoginPayload("fin007004", params);
      HttpServices.startApiReq(payload).then(res=>{
          if(res.msgcode == 1){
            store.commit('admin/account/SET_MENU_TREE',res.resultdata.menu)
            commit('remoteMenuSet',res.resultdata.menu)
          }
      })
    },
    // 获取远程的菜单数据
    getRemoteMenu({state,dispatch,commit}) {
      // 本地支持的所有头部菜单
      const fetchMenuData = storage.getStorage('menuTree')
      console.log("菜单是:",fetchMenuData)
      // fetchMenuData 菜单数据在实际使用的过程当中从API接口获取
      if (!fetchMenuData || fetchMenuData.length === 0) {
        // 获取到的远程菜单为空
        commit('remoteMenuSet',[])
        commit('asideSet',[])
        return Promise.resolve([])
      } else {
        // 实际渲染的头部菜单
        const renderHeader = []
        // fetchMenuData &&
        //   fetchMenuData.map(fetchItem => {
        //     fetchItem.path = fetchItem.MenuInfoPath
        //     fetchItem.title = fetchItem.MenuName
        //     fetchItem.iconSvg = fetchItem.MenuIcon
        //     renderHeader.push(fetchItem)
        //   })
        const renderMenuData = fetchMenuData
        // const homeItem = {
        //   MenuName:"首页",
        //   MenuPath:"/index",
        //   children:[],
        //   MenuIcon:"1.png",
        //   level:0,
        // }
        // renderMenuData.unshift(homeItem)
        // 1、循环一级菜单
        const renderMenu = renderMenuData
        commit('remoteMenuSet',renderMenu)
        commit('headerSet',renderHeader)
        // const db = dbModules()
        // db.set({
        //   dbName: "sys",
        //   path: "menu.aside",
        //   value: renderMenu[0].children,
        //   user: true
        // })
        storage.setStorage('menu.aside',renderMenu[0].children)
        commit('asideSet',renderMenu[0].children)
        store.dispatch('admin/page/init',frameInRoutes)
        return Promise.resolve(renderMenu)
      }
    },
    async asideLoad () {
      const menu = storage.getStorage('menu')
      this.aside = storage.getStorage('menu.aside')
      // console.log("拿到的数据是",this.aside)
      this.remoteMenu = storage.getStorage('headerMenu')
      this.asideActiveIndex = storage.getStorage('asideActiveIndex')
      // this.activeIndex = storage.getStorage('activeIndex')
      // this.asideCollapse =
      //   menu.asideCollapse !== undefined
      //     ? menu.asideCollapse
      //     : setting.menu.asideCollapse
      // this.asideTransition =
      //   menu.asideTransition !== undefined
      //     ? menu.asideTransition
      //     : setting.menu.asideTransition
    },
    /**
     * 动态切换左侧菜单栏
     * @param state
     * @param commit
     * @param path
     */
    switchAsideMenu (fullPath) {
      const remoteMenu = this.remoteMenu
      const commitAsideSet = shortPath => {
        if(remoteMenu.find(item => item.shortPath === shortPath)){
          this.aside = remoteMenu.find(item => item.shortPath === shortPath)?.children
        }
      }
      const splitPaths = fullPath.split('/')
      let shortPath = fullPath
      if (splitPaths && splitPaths.length >= 4) {
        shortPath = '/' + splitPaths.splice(3, splitPaths.length - 3).join('/')
        console.log('shortPath===', shortPath)
      }
      commitAsideSet(shortPath)
      return Promise.resolve()
    },
    activeMenuPathSet(path){
      this.activeMenuPath = path
    }
  },
  mutations: {
    SET_ACTION_INDEX(state,data){
      state.asideActiveIndex = data
    },
    /**
     * @description 设置顶栏菜单
     * @param {Object} state state
     * @param {Array} menu menu setting
     */
    headerSet (state, menu) {
      // store 赋值
      state.header = menu
      storage.setStorage('headerMenu', menu)
    },
    /**
     * @description 设置侧边栏菜单
     * @param {Object} state state
     * @param {Array} menu menu setting
     */
    asideSet (state, menu) {
      // store 赋值
      state.aside = menu
    },
    /**
     * @description 设置从服务端返回的菜单树
     * @param {Object} state vuex state
     * @param {Array} menu menu setting
     */
    remoteMenuSet (state, dataList) {
      // store 赋值
      state.remoteMenu = dataList
    },
    /**
     * @description 设置侧边栏被激活的菜单路径
     * @param {Object} state state
     * @param {String}
     */
    activeMenuPathSet (state, path) {
      // store 赋值
      state.activeMenuPath = path
    }
  }
}
